package com.scau.databaseexam.config;

import com.scau.databaseexam.component.LoginHandlerInterceptor;
import com.scau.databaseexam.component.MyLocalResolver;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class MyMvcConfig extends WebMvcConfigurationSupport {

    @Value("${web.upload-path}")
    private String uploadPath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        super.addResourceHandlers(registry);
        registry.addResourceHandler("/uploads/**").addResourceLocations(
                "file:"+uploadPath);
        registry.addResourceHandler("/asserts/**").addResourceLocations(
                ResourceUtils.CLASSPATH_URL_PREFIX + "/static/asserts/");
        registry.addResourceHandler("/bootstrap3/**").addResourceLocations(
                ResourceUtils.CLASSPATH_URL_PREFIX + "/static/bootstrap3/");
    }

    //拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //拦截所有
        //排除"excludePathPatterns"
        registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
                .excludePathPatterns("/index.html","/","/user/login", "/user/register","/register.html","/**/*.css",
                        "/**/*.js", "/**/*.png", "/**/*.jpg",
                        "/**/*.jpeg", "/**/*.gif", "/**/fonts/*");

    }

    //国际化
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocalResolver();
    }


}
